package io.acna.basedao.mybatis.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryCondition implements Serializable {
	public enum betweenType {
		/**
		 * 可以等于(a >= startValue and a <= endValue )，包含startValue和endValue
		 */
		OREQUARY,
		/**
		 * 不能等于(a > startValue and a < endValue )，不包含startValue和endValue
		 */
		NOTEQUARY
	}

	public enum OrderByType {
		/**
		 * 升序
		 */
		ASC,
		/**
		 * 降序
		 */
		DESC
	}

	public enum MultiConditionsType {
		AND, OR
	}

	public enum ConnectType {
		AND(" AND "), WHERE(" WHERE ");
		ConnectType(String value) {
			this.value = value;
		}

		private String value;

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

	}

	public QueryCondition() {
		connectFlag = ConnectType.WHERE;
		type = MultiConditionsType.AND;
	}

	private MultiConditionsType type;
	private Map<String, OrderByType> orderBy;
	private Map<String, Object> isNotConditions = new HashMap<String, Object>();
	private Map<String, Object> sqlParams = new HashMap<String, Object>();
	private ConnectType connectFlag;

	/**
	 * @author cuilei
	 * @description 设置查询条件ORDERBY排序
	 * @info createTime 2015年3月26日 上午10:10:52<br>
	 *       updateAuthor cuilei<br>
	 *       updateTime 2015年3月26日 上午10:10:52
	 * @param columnName
	 *            需要排序的列名
	 * @param type
	 *            降序/升序[DESC/ASC]
	 */
	public void orderBy(String columnName, OrderByType type) {
		orderBy = new HashMap<String, QueryCondition.OrderByType>();
		orderBy.put(columnName, type);
	}

	public void orQuery() {
		this.type = MultiConditionsType.OR;
	}

	public void addSqlParam(String key, Object value) {
		this.sqlParams.put(key, value);
	}

	/**
	 * 是否模糊查询
	 */
	private Boolean isLike = false;

	/**
	 * 是否查询全部[忽视分页]
	 */
	private Boolean isQueryAll = false;

	public void addSearchContions(String name, Object value) {
		this.searchContions.put(name, value);
	}

	/**
	 *
	 * @author Huangyuancheng
	 * @description 判断属性不为空
	 * @info createTime 2015年4月2日 下午4:40:19<br>
	 *       updateAuthor Huangyuancheng<br>
	 *       updateTime 2015年4月2日 下午4:40:19
	 * @param name
	 */
	public void addSearchIsNotNull(String name, Object value) {
		this.searchContions.put(name, value);
	}

	/**
	 * @author cuilei
	 * @description 添加不等于条件
	 * @info createTime 2015年3月26日 上午11:37:56<br>
	 *       updateAuthor cuilei<br>
	 *       updateTime 2015年3月26日 上午11:37:56
	 * @param name
	 * @param value
	 */
	public void addIsNotContions(String name, Object value) {
		this.isNotConditions.put(name, value);
	}

	private List<String> betweenConditions = new ArrayList<String>();

	public void addBetweenConditions(String columnName, Double startValue,
			Double endValue, betweenType type) {
		String tempSql = "";
		String compareLeftFlag = ">";
		String compareRightFlag = "<";
		switch (type) {
		case NOTEQUARY:
			break;
		case OREQUARY:
			compareLeftFlag += "=";
			compareRightFlag += "=";
			break;
		}

		tempSql = columnName + compareLeftFlag + startValue + " and "
				+ columnName + compareRightFlag + endValue;
		this.betweenConditions.add(tempSql);
	}

	public void addBetweenConditions(String columnName, int startValue,
			int endValue, betweenType type) {
		String tempSql = "";
		String compareLeftFlag = ">";
		String compareRightFlag = "<";
		switch (type) {
		case NOTEQUARY:
			break;
		case OREQUARY:
			compareLeftFlag += "=";
			compareRightFlag += "=";
			break;
		}

		tempSql = columnName + compareLeftFlag + startValue + " and "
				+ columnName + compareRightFlag + endValue;
		this.betweenConditions.add(tempSql);
	}

	public QueryCondition(Boolean isLike) {
		super();
		this.isLike = isLike;
	}

	/**
	 * EasyUI分页参数 page:页码
	 */
	private int page = 1;
	/**
	 * EasyUI分页参数rows:表格显示行数
	 */
	private int rows = 30;

	/**
	 * 按照Value进行模糊搜索的条件集合
	 */
	Map<String, Object> searchContions = new HashMap<String, Object>();

	/**
	 * @author cuilei
	 * @description 无视分页参数[查询全部]
	 * @info createTime 2015年3月25日 下午8:47:29<br>
	 *       updateAuthor cuilei<br>
	 *       updateTime 2015年3月25日 下午8:47:29
	 */
	public void isQueryAll() {
		this.isQueryAll = true;
	}

	/**
	 * @author cuilei
	 * @description 设置like查询，非等值查询(前后模糊查询)
	 * @info createTime 2015年3月25日 下午8:47:51<br>
	 *       updateAuthor cuilei<br>
	 *       updateTime 2015年3月25日 下午8:47:51
	 */
	public void isLike() {
		this.isLike = true;
	}

	public RowBound getRowBound() {
		if (isQueryAll)
			return new RowBound(0, 0);
		return new RowBound(page, rows);
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public Map<String, Object> getSearchContions() {
		return searchContions;
	}

	public Map<String, OrderByType> getOrderBy() {
		return orderBy;
	}

	public void setSearchContions(Map<String, Object> searchContions) {
		this.searchContions = searchContions;
	}

	public Boolean getIsLike() {
		return isLike;
	}

	public void setIsLike(Boolean isLike) {
		this.isLike = isLike;
	}

	public List<String> getBetweenConditions() {
		return betweenConditions;
	}

	public void setBetweenConditions(List<String> betweenConditions) {
		this.betweenConditions = betweenConditions;
	}

	public Map<String, Object> getIsNotConditions() {
		return isNotConditions;
	}

	public MultiConditionsType getType() {
		return type;
	}

	public Map<String, Object> getSqlParams() {
		return sqlParams;
	}

	public Boolean getIsQueryAll() {
		return isQueryAll;
	}

	public void setIsQueryAll(Boolean isQueryAll) {
		this.isQueryAll = isQueryAll;
	}

	public void setType(MultiConditionsType type) {
		this.type = type;
	}

	public void setOrderBy(Map<String, OrderByType> orderBy) {
		this.orderBy = orderBy;
	}

	public void setIsNotConditions(Map<String, Object> isNotConditions) {
		this.isNotConditions = isNotConditions;
	}

	public void setSqlParams(Map<String, Object> sqlParams) {
		this.sqlParams = sqlParams;
	}

	public ConnectType getConnectFlag() {
		return connectFlag;
	}

	public void setConnectFlag(ConnectType connectFlag) {
		this.connectFlag = connectFlag;
	}

	public void isAnd() {
		this.connectFlag = ConnectType.AND;
	}

	@Override
	public String toString() {
		return "QueryCondition [type=" + type + ", orderBy=" + orderBy
				+ ", isNotConditions=" + isNotConditions + ", sqlParams="
				+ sqlParams + ", connectFlag=" + connectFlag + ", isLike="
				+ isLike + ", isQueryAll=" + isQueryAll
				+ ", betweenConditions=" + betweenConditions + ", page=" + page
				+ ", rows=" + rows + ", searchContions=" + searchContions + "]";
	}

}
